//
//  HQDataBaseOperate.m
//  OldHandPT
//
//  Created by ben on 2018/12/14.
//  Copyright © 2018 Shouny. All rights reserved.
//

#import "HQDataBaseOperate.h"

@implementation HQDataBaseOperate
# pragma mark 单例
+(HQDataBaseOperate *)sharedDataBase{
    static HQDataBaseOperate *loginDataBase = nil;
    if (!loginDataBase) {
        loginDataBase = [[HQDataBaseOperate alloc] init];
    }
    return loginDataBase;
}
- (id)init{
    self = [super init];
    if (self) {
        [self checkPublicDatabase];
    }
    return self;
}

#pragma mark 检测SandBox中是否有公共数据库文件，没有从资源库拷贝到SandBox
- (void)checkPublicDatabase{
    NSString *dbName = @"OldHand.sqlite";
    [self checkDatabase:dbName targetName:dbName publicDB:YES];
}

#pragma mark 从资源库拷贝到SandBox
- (void)checkDatabase:(NSString *)orginName targetName:(NSString *)targetName publicDB:(BOOL)publicDB{
    BOOL success;
    NSString *dbFile = orginName;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"数据库路径   %@",documentsDirectory);
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:targetName];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (!success) {
        // 检测资源库里面是否有数据库文件
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbFile];
        success = [fileManager fileExistsAtPath:defaultDBPath];
        if (success) {
            // 将数据库文件从资源库拷贝到SandBox
            success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
            if (!success) {
                NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
            }
        } else {
            NSLog(@"资源库中没有数据库文件：%@", defaultDBPath);
        }
    }
    if (success) {//沙盒中存在数据库文件
        FMDatabase *database = [[FMDatabase alloc] initWithPath:writableDBPath];
        if (publicDB) {  // 如果传入YES ，建好的数据库赋值给_publicDB
            _publicDB = database;
        } else {        // 如果传入的NO ,建好的数据库赋值给_userDB
            _userDB = database;
        }
        if (![database open]) {
            NSLog(@"open db failed.");
        }
    }
}

#pragma mark - C1
// 更新&&插入
+(BOOL)queryCOneWithFromUserID:(COneAnsionModel *)model
{
    if (!model) {
        NSLog(@"%s %i 输入异常异常", __FUNCTION__, __LINE__);
        return NO;
    }
    BOOL flag = NO;
    //参数化
    NSDictionary *argsDict = @{
                               @"_id":model._id?:@"",
                               @"answer":model.answer?:@"",
                               @"explains":model.explains?:@"",
                               @"item1":model.item1?:@"",
                               @"item2":model.item2?:@"",
                               @"item3":model.item3?:@"",
                               @"item4":model.item4?:@"",
                               @"question":model.question?:@"",
                               @"url":model.url?:@"",
                               };
    flag = [[HQDataBaseOperate sharedDataBase].publicDB executeUpdate:@"replace into C_One_tab (_id,answer,explains,item1,item2,item3,item4,question,url) VALUES (:_id,:answer,:explains,:item1,:item2,:item3,:item4,:question,:url)" withParameterDictionary:argsDict];
    if (!flag) {
        NSLog(@"%s %i %@", __FUNCTION__, __LINE__, [HQDataBaseOperate sharedDataBase].publicDB.lastError);
    }
    return flag;
}

// 获取所有数据
+(NSArray *)queryAllCOneMessage{
    NSMutableArray *userArray = [NSMutableArray array];
    // 更新
    NSString *sql = @"SELECT * FROM C_One_tab";
    FMResultSet *rs = [[HQDataBaseOperate sharedDataBase].publicDB executeQuery:sql];
    while ([rs next]) {
        //查询结果转化为字典。
        NSDictionary *dic = [rs resultDictionary];
        COneAnsionModel *model =   [COneAnsionModel yy_modelWithDictionary:dic];
        [userArray addObject:model];
    }
    [rs close];
    return userArray;
}

#pragma mark - C2
+(BOOL)queryCTwoWithFromUserID:(COneAnsionModel *)model
{
    if (!model) {
        NSLog(@"%s %i 输入异常异常", __FUNCTION__, __LINE__);
        return NO;
    }
    BOOL flag = NO;
    //参数化
    NSDictionary *argsDict = @{
                               @"_id":model._id?:@"",
                               @"answer":model.answer?:@"",
                               @"explains":model.explains?:@"",
                               @"item1":model.item1?:@"",
                               @"item2":model.item2?:@"",
                               @"item3":model.item3?:@"",
                               @"item4":model.item4?:@"",
                               @"question":model.question?:@"",
                               @"url":model.url?:@"",
                               };
    flag = [[HQDataBaseOperate sharedDataBase].publicDB executeUpdate:@"replace into C_Two_tab (_id,answer,explains,item1,item2,item3,item4,question,url) VALUES (:_id,:answer,:explains,:item1,:item2,:item3,:item4,:question,:url)" withParameterDictionary:argsDict];
    if (!flag) {
        NSLog(@"%s %i %@", __FUNCTION__, __LINE__, [HQDataBaseOperate sharedDataBase].publicDB.lastError);
    }
    return flag;
}

+(NSArray *)queryAllCTwoMessage{
    NSMutableArray *userArray = [NSMutableArray array];
    // 更新
    NSString *sql = @"SELECT * FROM C_Two_tab";
    FMResultSet *rs = [[HQDataBaseOperate sharedDataBase].publicDB executeQuery:sql];
    while ([rs next]) {
        //查询结果转化为字典。
        NSDictionary *dic = [rs resultDictionary];
        COneAnsionModel *model =   [COneAnsionModel yy_modelWithDictionary:dic];
        [userArray addObject:model];
    }
    [rs close];
    return userArray;
}

#pragma mark - B1
+(BOOL)queryBOneWithFromUserID:(COneAnsionModel *)model
{
    if (!model) {
        NSLog(@"%s %i 输入异常异常", __FUNCTION__, __LINE__);
        return NO;
    }
    BOOL flag = NO;
    //参数化
    NSDictionary *argsDict = @{
                               @"_id":model._id?:@"",
                               @"answer":model.answer?:@"",
                               @"explains":model.explains?:@"",
                               @"item1":model.item1?:@"",
                               @"item2":model.item2?:@"",
                               @"item3":model.item3?:@"",
                               @"item4":model.item4?:@"",
                               @"question":model.question?:@"",
                               @"url":model.url?:@"",
                               };
    flag = [[HQDataBaseOperate sharedDataBase].publicDB executeUpdate:@"replace into B_One_tab (_id,answer,explains,item1,item2,item3,item4,question,url) VALUES (:_id,:answer,:explains,:item1,:item2,:item3,:item4,:question,:url)" withParameterDictionary:argsDict];
    if (!flag) {
        NSLog(@"%s %i %@", __FUNCTION__, __LINE__, [HQDataBaseOperate sharedDataBase].publicDB.lastError);
    }
    return flag;
}

+(NSArray *)queryAllBOneMessage{
    NSMutableArray *userArray = [NSMutableArray array];
    // 更新
    NSString *sql = @"SELECT * FROM B_One_tab";
    FMResultSet *rs = [[HQDataBaseOperate sharedDataBase].publicDB executeQuery:sql];
    while ([rs next]) {
        //查询结果转化为字典。
        NSDictionary *dic = [rs resultDictionary];
        COneAnsionModel *model =   [COneAnsionModel yy_modelWithDictionary:dic];
        [userArray addObject:model];
    }
    [rs close];
    return userArray;
}

#pragma mark - B2
+(BOOL)queryBTwoWithFromUserID:(COneAnsionModel *)model
{
    if (!model) {
        NSLog(@"%s %i 输入异常异常", __FUNCTION__, __LINE__);
        return NO;
    }
    BOOL flag = NO;
    //参数化
    NSDictionary *argsDict = @{
                               @"_id":model._id?:@"",
                               @"answer":model.answer?:@"",
                               @"explains":model.explains?:@"",
                               @"item1":model.item1?:@"",
                               @"item2":model.item2?:@"",
                               @"item3":model.item3?:@"",
                               @"item4":model.item4?:@"",
                               @"question":model.question?:@"",
                               @"url":model.url?:@"",
                               };
    flag = [[HQDataBaseOperate sharedDataBase].publicDB executeUpdate:@"replace into B_Two_tab (_id,answer,explains,item1,item2,item3,item4,question,url) VALUES (:_id,:answer,:explains,:item1,:item2,:item3,:item4,:question,:url)" withParameterDictionary:argsDict];
    if (!flag) {
        NSLog(@"%s %i %@", __FUNCTION__, __LINE__, [HQDataBaseOperate sharedDataBase].publicDB.lastError);
    }
    return flag;
}

+(NSArray *)queryAllBTwoMessage{
    NSMutableArray *userArray = [NSMutableArray array];
    // 更新
    NSString *sql = @"SELECT * FROM B_Two_tab";
    FMResultSet *rs = [[HQDataBaseOperate sharedDataBase].publicDB executeQuery:sql];
    while ([rs next]) {
        //查询结果转化为字典。
        NSDictionary *dic = [rs resultDictionary];
        COneAnsionModel *model =   [COneAnsionModel yy_modelWithDictionary:dic];
        [userArray addObject:model];
    }
    [rs close];
    return userArray;
}

#pragma mark - A1
+(BOOL)queryAOneWithFromUserID:(COneAnsionModel *)model
{
    if (!model) {
        NSLog(@"%s %i 输入异常异常", __FUNCTION__, __LINE__);
        return NO;
    }
    BOOL flag = NO;
    //参数化
    NSDictionary *argsDict = @{
                               @"_id":model._id?:@"",
                               @"answer":model.answer?:@"",
                               @"explains":model.explains?:@"",
                               @"item1":model.item1?:@"",
                               @"item2":model.item2?:@"",
                               @"item3":model.item3?:@"",
                               @"item4":model.item4?:@"",
                               @"question":model.question?:@"",
                               @"url":model.url?:@"",
                               };
    flag = [[HQDataBaseOperate sharedDataBase].publicDB executeUpdate:@"replace into A_One_tab (_id,answer,explains,item1,item2,item3,item4,question,url) VALUES (:_id,:answer,:explains,:item1,:item2,:item3,:item4,:question,:url)" withParameterDictionary:argsDict];
    if (!flag) {
        NSLog(@"%s %i %@", __FUNCTION__, __LINE__, [HQDataBaseOperate sharedDataBase].publicDB.lastError);
    }
    return flag;
}

+(NSArray *)queryAllAOneMessage{
    NSMutableArray *userArray = [NSMutableArray array];
    // 更新
    NSString *sql = @"SELECT * FROM A_One_tab";
    FMResultSet *rs = [[HQDataBaseOperate sharedDataBase].publicDB executeQuery:sql];
    while ([rs next]) {
        //查询结果转化为字典。
        NSDictionary *dic = [rs resultDictionary];
        COneAnsionModel *model =   [COneAnsionModel yy_modelWithDictionary:dic];
        [userArray addObject:model];
    }
    [rs close];
    return userArray;
}

#pragma mark - A2
+(BOOL)queryATwoWithFromUserID:(COneAnsionModel *)model
{
    if (!model) {
        NSLog(@"%s %i 输入异常异常", __FUNCTION__, __LINE__);
        return NO;
    }
    BOOL flag = NO;
    //参数化
    NSDictionary *argsDict = @{
                               @"_id":model._id?:@"",
                               @"answer":model.answer?:@"",
                               @"explains":model.explains?:@"",
                               @"item1":model.item1?:@"",
                               @"item2":model.item2?:@"",
                               @"item3":model.item3?:@"",
                               @"item4":model.item4?:@"",
                               @"question":model.question?:@"",
                               @"url":model.url?:@"",
                               };
    flag = [[HQDataBaseOperate sharedDataBase].publicDB executeUpdate:@"replace into A_Two_tab (_id,answer,explains,item1,item2,item3,item4,question,url) VALUES (:_id,:answer,:explains,:item1,:item2,:item3,:item4,:question,:url)" withParameterDictionary:argsDict];
    if (!flag) {
        NSLog(@"%s %i %@", __FUNCTION__, __LINE__, [HQDataBaseOperate sharedDataBase].publicDB.lastError);
    }
    return flag;
}

+(NSArray *)queryAllATwoMessage{
    NSMutableArray *userArray = [NSMutableArray array];
    // 更新
    NSString *sql = @"SELECT * FROM A_Two_tab";
    FMResultSet *rs = [[HQDataBaseOperate sharedDataBase].publicDB executeQuery:sql];
    while ([rs next]) {
        //查询结果转化为字典。
        NSDictionary *dic = [rs resultDictionary];
        COneAnsionModel *model =   [COneAnsionModel yy_modelWithDictionary:dic];
        [userArray addObject:model];
    }
    [rs close];
    return userArray;
}

@end
